package top.milkbox.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.milkbox.domain.MenuSimple;
import top.milkbox.domain.Userinfo;
import top.milkbox.service.MenuSimpleService;
import top.milkbox.utils.R;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/menus")
public class MenuController {

    @Autowired
    private MenuSimpleService menuSimpleService;

    @GetMapping
    public R findAll() {
        List<MenuSimple> menuList = menuSimpleService.list();
        return new R(true, menuList, "操作成功");
    }

    /**
     * 根据id和用户session查询菜的信息，包含点击量点赞量和收藏量以及用户是否收藏和点赞
     *
     * @param id      菜id
     * @param session 用户session
     * @return R 对象
     */
    @GetMapping("/{id}")
    public R findOne(@PathVariable("id") Integer id, HttpSession session) {
        Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
        R r = new R();
        if (userinfo == null) {
            r.setLogin(false);
            r.setFlag(false);
            r.setMessage("用户未登录");
        } else {
            MenuSimple menu = menuSimpleService.findOneById(id, session);
            r.setFlag(true);
            r.setLogin(true);
            r.setData(menu);
            r.setMessage("查询成功");
        }
        return r;
    }

    /**
     * 根据食材和制作工具模糊查询
     * 旧地址：@GetMapping("/{stuff}/{tools}")
     *
     * @param stuff 食材
     * @param tools 制作工具
     * @return 菜单列表
     */
//    @GetMapping("/{stuff}/{tools}")
    @GetMapping("/like/{stuff}/{tools}")
    public R findLike(
            @PathVariable("stuff") List<String> stuff,
            @PathVariable("tools") List<String> tools
    ) {
        List<MenuSimple> menuList = menuSimpleService.findByToolsAndStuffLike(tools, stuff);
        return new R(true, menuList, "操作成功");
    }

    /**
     * 查询所有菜，通过点击量排序
     */
    @GetMapping("/order")
    public R findAllOrderByClick() {
        List<MenuSimple> menuList = menuSimpleService.findAllOrderByClickCount();
        return new R(true, menuList, "查询成功");
    }

    /**
     * 根据不同性别，查询最受欢迎的菜
     * 0:未知，1:男，2:女
     */
    @GetMapping("/order/{gender}")
    public R findAllByGender(@PathVariable("gender") Integer gender) {
        List<MenuSimple> menuSimpleList = menuSimpleService.findAllByGender(gender);
        return new R(true, menuSimpleList, "查询成功");
    }


}
